package com.yanqu.road.server.gameplayer.module.activity.businessdepart;

import com.yanqu.road.dao.impl.activity.businessdepart.BusinessDepartDailyActiveDaoImpl;
import com.yanqu.road.dao.impl.activity.businessdepart.BusinessDepartUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.ActivityValueParamArgs;
import com.yanqu.road.entity.activity.businessdepart.BusinessDepartDailyActive;
import com.yanqu.road.entity.activity.businessdepart.BusinessDepartUserData;
import com.yanqu.road.entity.activity.businessdepart.config.BusinessDepartConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.businessdepart.BusinessDepartMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BusinessDepartModule extends GeneralModule {

    /**
     * 玩家主数据
     */
    private Map<Integer, BusinessDepartUserData> businessDepartUserDataMap = new ConcurrentHashMap<>();

    /**
     * 玩家每日活跃值数据。k:活动ID,k:日期
     */
    private Map<Integer, Map<String, BusinessDepartDailyActive>> businessDepartDailyActiveMap = new ConcurrentHashMap<>();

    public BusinessDepartModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        this.businessDepartUserDataMap = new BusinessDepartUserDataDaoImpl().getData(player.getUserId());
        this.businessDepartDailyActiveMap = new BusinessDepartDailyActiveDaoImpl().getData(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        //存储玩家数据
        BusinessDepartUserDataDaoImpl userDataDao = new BusinessDepartUserDataDaoImpl();
        for (BusinessDepartUserData data : this.businessDepartUserDataMap.values()) {
            if (data.isInsertOption()) {
                userDataDao.add(data);
            } else if (data.isUpdateOption()) {
                userDataDao.update(data);
            }
        }

        //存储每日活跃数据
        BusinessDepartDailyActiveDaoImpl dailyActiveDao = new BusinessDepartDailyActiveDaoImpl();
        for (Map<String, BusinessDepartDailyActive> dataMap : this.businessDepartDailyActiveMap.values()) {
            for (BusinessDepartDailyActive data : dataMap.values()) {
                if (data.isInsertOption()) {
                    dailyActiveDao.add(data);
                } else if (data.isUpdateOption()) {
                    dailyActiveDao.update(data);
                }
            }
        }
        return true;
    }

    /**
     * 获取玩家数据
     * @param activityId
     * @return
     */
    private synchronized BusinessDepartUserData getUserData(int activityId) {
        BusinessDepartUserData userData = this.businessDepartUserDataMap.get(activityId);
        if (userData == null) {
            userData = new BusinessDepartUserData(player.getUserId(), activityId);
            this.businessDepartUserDataMap.put(activityId, userData);
        }
        return userData;
    }

    /**
     * 获取活跃天数
     * @return
     */
    private int getActiveDayNum(int activityId) {
        Map<String, BusinessDepartDailyActive> dataMap = this.businessDepartDailyActiveMap.get(activityId);
        if (dataMap == null || dataMap.isEmpty()) {
            return 0;
        }
        BusinessDepartConfig businessDepartConfig = BusinessDepartMgr.getConfig(activityId);
        if (businessDepartConfig == null) {
            return 0;
        }
        //获取一下每日活跃达成值
        long requestNum = businessDepartConfig.getDEPART_DAILY_ACTIVITY_REQUEST();
        int activeDayNum = 0;
        for (BusinessDepartDailyActive data : dataMap.values()) {
            if (data.getActive() >= requestNum) {
                activeDayNum++;
            }
        }
        return activeDayNum;
    }

    /**
     * 更新每日活跃值
     * @param active
     */
    public synchronized void updateDailyActive(long active) {
        //判断系统是否解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.BusinessDepart.getValue())) {
            return;
        }
        long now = System.currentTimeMillis();
        String date = DateHelper.getYMDBARString(now);
        Map<Integer, BusinessDepartConfig> activityConfigMap = BusinessDepartMgr.getConfigMap();
        for (BusinessDepartConfig activityConfig : activityConfigMap.values()) {
            //判断活动是否结束
            ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityConfig.getActivityInfo().getActivityId());
            if (activityInfo == null) {
                continue;
            }
            Map<String, BusinessDepartDailyActive> dataMap = this.businessDepartDailyActiveMap.computeIfAbsent(activityInfo.getActivityId(), k -> new ConcurrentHashMap<>());
            BusinessDepartDailyActive businessDepartDailyActive = dataMap.get(date);
            if (businessDepartDailyActive == null) {
                //new一下
                businessDepartDailyActive = new BusinessDepartDailyActive(
                        this.getUserId(),
                        activityInfo.getActivityId(),
                        date,
                        active,
                        now);
                dataMap.put(date, businessDepartDailyActive);
            } else {
                //更新一下
                if (active > businessDepartDailyActive.getActive()) {
                    businessDepartDailyActive.setActive(active);
                }
            }
            //获取一下活跃天数
            int activeDayNum = this.getActiveDayNum(activityInfo.getActivityId());
            //notify一下任务
            ActivityValueParamArgs args = new ActivityValueParamArgs(activityInfo.getActivityId(), activeDayNum, "");
            player.notifyListener(eGamePlayerEventType.BusinessDepartActiveTask.getValue(), args);
        }
    }

    /**
     * 添加累计积分消耗
     * @param goodsId
     * @param costNum
     */
    public synchronized void addCostNum(int goodsId, long costNum) {
        //判断系统是否解锁
        if (!SystemOpenMgr.systemOpen(player, eSystemId.BusinessDepart.getValue())) {
            return;
        }
        //获取活动配置
        Map<Integer, BusinessDepartConfig> activityConfigMap = BusinessDepartMgr.getConfigMap();
        for (BusinessDepartConfig activityConfig : activityConfigMap.values()) {
            //判断活动是否结束
            ActivityInfo activityInfo = ActivityMgr.getOpenActivityInfo(activityConfig.getActivityInfo().getActivityId());
            if (activityInfo == null) {
                continue;
            }
            //判断积分道具是否一致
            if (activityConfig.getEXCHANGE_SCORE_ITEM_ID() != goodsId) {
                continue;
            }
            BusinessDepartUserData userData = this.getUserData(activityInfo.getActivityId());
            userData.setCostNum(userData.getCostNum() + costNum);
            //notify一下任务
            ActivityValueParamArgs args = new ActivityValueParamArgs(activityInfo.getActivityId(), userData.getCostNum(), "");
            player.notifyListener(eGamePlayerEventType.BusinessDepartCostScoreTask.getValue(), args);
        }
    }
}
